<?php

namespace app\api\controller;

use app\api\model\Labels as LabelsModel;
use app\api\model\LabelsUser;
use think\response\Json;
use think\Validate;
use think\Db;

class Labels extends Common
{
    public function lists(): Json
    {
        $LabelsModel = new LabelsModel;

        $where = ['parent_id' => 0, 'is_hidden' => 0];

        $data = $LabelsModel->all(function($query) use ($where) {
            $query->field('id, name, icon')->where($where)->order('sort desc');
        });

        if ($data) {
            $parent_ids = array_column($data, 'id');

            $where['parent_id'] = ['in', $parent_ids];

            $contain = $LabelsModel->where($where)->order('sort desc')->column('id, parent_id, name, icon');

            $child = [];
            foreach ($contain as $item) {
                $mata = $item;
                unset($mata['parent_id']);
                $child[$item['parent_id']][] = $mata;
            }

            foreach ($data as $key => $item) {
                if (isset($child[$item['id']])) {
                    $data[$key]['child'] = $child[$item['id']];
                }
            }
        }

        return $this->pass($data);
    }

    public function edit()
    {
        LabelsModel::edit($this->params['labels'], $this->member['id'], $this->params['types']);

        return $this->return['common.success.update'];
    }

    public function myLabels(): Json
    {
        $label_ids = (new LabelsUser)->where(['types' => $this->params['types'], 'user_id' => $this->member['id']])->column('label_id');

        if ($label_ids) {
            $data = (new LabelsModel)->field('id, name')->where(['id' => ['in', $label_ids], 'is_hidden' => 0])->select();
        } else {
            $data = [];
        }

        return $this->pass($data);
    }

    public function Create($title = '', $icon = '', $parent_id = '', $description = '')
    {
        $rule = [
            'title'    => 'require|max:60',
            'icon'  => 'require',
            'parent_id'     => 'require',
            'description'    => 'max:255',
        ];

        $msg = [
            'title.require'    => '名称必填',
            'title.max'        => '名称最多不能超过60个字符',
            'icon.require'  => '图标必须上传',
            'parent_id.require'  => '必须选择父级分类',
            'description.max'    => '原因不能超过60个字符',
        ];
        $data = [
            'title'    => $title,
            'icon'    => $icon,
            'parent_id'    => $parent_id,
            'description'    => $description,
            'user_id'    => $this->member['id'],
            'create_time'   => time()
        ];

        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)) {
            return $this->miss($validate->getError());
        }
        $parent = Db::name('labels')->where(['id'=>$parent_id, 'parent_id'=>0])->find();
        if (!$parent) {
            return $this->miss('父级分类不存在');
        }
        $has = Db::name('labels')->where(['parent_id'=>$parent_id, 'name'=>$title])->find();
        if ($has) {
            return $this->miss('名称已经存在');
        }

        $res = Db::name('label_create_apply')->insert($data);
        if ($res) {
            return $this->pass('创建成功等待审核');
        }
        return $this->miss('操作失败请重试');
    }
}
